package cn.bqjr.platform.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class MyAbstractRoutingDataSource extends AbstractRoutingDataSource {  
  
  
    //private final int dataSourceNumber;  
    //private AtomicInteger count = new AtomicInteger(0);  
  
  
    public MyAbstractRoutingDataSource(int dataSourceNumber) {  
        //this.dataSourceNumber = dataSourceNumber;  
    }  
  
  
    @Override  
    protected Object determineCurrentLookupKey() {  
        String typeKey = DataSourceContextHolder.getDataSourceType();  
        if (typeKey.equals(DataSourceType.write1.getType()))  
            return DataSourceType.write1.getType();  
        if (typeKey.equals(DataSourceType.write2.getType()))  
            return DataSourceType.write2.getType();  
        if (typeKey.equals(DataSourceType.read1.getType()))  
            return DataSourceType.read1.getType();  
        if (typeKey.equals(DataSourceType.read2.getType()))  
            return DataSourceType.read2.getType();  
        return DataSourceType.read1.getType();  //默认读1库
        // 读 简单负载均衡  
        //int number = count.getAndAdd(1);  
        //int lookupKey = number % dataSourceNumber;  
        //return new Integer(lookupKey);  
    }  
}  
